layui.use(['element','layer'],function () {
    var element = layui.element;
    var $ = layui.$;
    var layer = layui.layer;
    //获取未读消息数
    $.ajax({
       type:"POST"
        ,url:"/getUnReadCount"
        ,success:function (res) {
            var count = res.data.count;
            $("#count").text(count);
        }
        ,error:function () {
            layer.msg("系统错误！");
        }

    });
    
    var active = {
        tabAdd:function (url,title,id) {
            element.tabAdd('tabs',{
                title:title,
                content:"<iframe dat-frameid='"+id+"' src='"+url+"'  frameborder='0'  style='width:100%;height: 100%'></iframe>",
                id:id
            });
            rightMenu();
            iframeWH();
        },
        tabChange:function (id) {
            element.tabChange('tabs',id);
        },
        tabDelete:function(id){
            element.tabDelete('tabs',id);
        },
        tabDeleteAll:function(ids){
            $.each(ids,function(index,item){
                element.tabDelete('tabs',item);
            });
        }
    };

    function rightMenu(){

        $(".layui-tab-title li").on("contextmenu",function (e) {
            var rightmenu = $(".rightmenu");
            rightmenu.find("li").attr('data-id',$(this).attr("lay-id"));
            l = e.clientX;
            t = e.clientY;
            rightmenu.css({ left:l, top:t}).show();
            return false;
        });
        //左键点击隐藏
        $("body,.layui-tab-title li").click(function(){
            $(".rightmenu").hide();
        });

    }



    function iframeWH(){
        var H = $(window).height()-80;
        $("iframe").css("height",H+"px");
    }


    $(".tab-menu").on("click",function () {
        var menu = $(this);
        /**
         *已打开的标签页数openTabLength如果小于等于，直接创建新的标签页，否则需要判断标签页是否存在如果存在则不创建直接跳转。
         */
        var openTabLength = $(".layui-tab-title li").length;
        if(openTabLength<=0){
            active.tabAdd(menu.attr("data-url"),menu.attr("data-title"),menu.attr("data-id"));
        }else{
            var isOpen = false;
            $.each($(".layui-tab-title li"),function () {
                if($(this).attr("lay-id") == menu.attr("data-id")){
                    isOpen = true;
                }
            })
            if(!isOpen){
                active.tabAdd(menu.attr("data-url"),menu.attr("data-title"),menu.attr("data-id"));
            }
            active.tabChange(menu.attr("data-id"));
        }

    });

    $("#logout").on("click",function () {
        layer.confirm('是否注销？', {
            btn: ['是','否'] //按钮
        }, function(){
            location.href = "/logout";
        }, function(){
        });

    })


    $(".rightmenu li").click(function(){
        if($(this).attr("data-type")=="closethis"){
            active.tabDelete($(this).attr("data-id"));
        }else if($(this).attr("data-type")=="closeall"){
            var tabtitle = $(".layui-tab-title li");
            var ids = new Array();
            tabtitle.each(function(i){
                ids.push($(this).attr("lay-id"));
            });
            //如果关闭所有 ，即将所有的lay-id放进数组，执行tabDeleteAll
            active.tabDeleteAll(ids);
        }
        $('.rightmenu').hide(); //最后再隐藏右键菜单
    });


    $(".icon").on("click",function () {

        var icon = $(this);
        if(icon.hasClass("layui-icon-shrink-right")){
            /**
             * 说明左侧菜单是显示的，需要隐藏菜单
             */
            // $(".nav-left ").css("display","none");
            //侧边栏伸缩隐藏
            $(".layui-nav-side").animate({left:'-200px'},100);
            //tab页面调整
            $(".layui-tab").css("cssText","margin-left:0px !important;margin-top:0px");
            icon.removeClass("layui-icon-shrink-right");
            icon.addClass("layui-icon-spread-left");
        }else{
            /**
             *  说明左侧菜单是隐藏的,需要显示
             */

            //侧边栏伸缩显示
            $(".layui-nav-side").animate({left:'0px'},100);
            //tab页面全屏显示
            $(".layui-tab").css("cssText","margin-left:200px !important;margin-top:0px");
            icon.removeClass("layui-icon-spread-left");
            icon.addClass("layui-icon-shrink-right");
            // $(".nav-left ").css("display","");
        }

    })

});





window.onresize = function (ev) {

    var width = document.body.clientWidth;
    var type;

    if(width<768){
        //移动端
        type = "xs";
    }else if(width>=768 && width<992){
        //平板
        type = "sm";
    }else if(width>=992 && width<1200){
        //桌面
        type = "md";
    }else if(width>=1200){
        //大型屏幕
        type = "lg";
    }

    if("xs"==type){
        $(".logo ").css("display","none");
        $(".icon ").css("display","none");
        //隐藏侧边导航栏
        $(".nav-left ").css("display","none");

        $(".layui-tab-card").css("margin-left","0px");
    }else{
        $(".logo ").css("display","");
        $(".icon ").css("display","");
        $(".layui-tab-card").css("margin-left","200px");
        //显示侧边导航栏
        $(".nav-left ").css("display","");
    }


}
